package com.example.sources.util;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Iterator;
import java.util.Properties;

/**
 * Created by Administrator on 2016/9/19.
 */
public class PropertiesUtil {

    public static String getProperties(String propertiesKey){
        Properties prop = new Properties();
        try{
            String filepath = Thread.currentThread().getContextClassLoader().getResource("")+ File.separator+"config.properties";


            filepath = eliminatePrefix(filepath);

            //读取属性文件a.properties
            InputStream in = new BufferedInputStream(new FileInputStream(filepath));
            prop.load(in);     ///加载属性列表
            Iterator<String> it=prop.stringPropertyNames().iterator();
            while(it.hasNext()){
                String key=it.next();
                if (key.equals(propertiesKey)){
                    return prop.getProperty(key);
                }
            }
            in.close();
        }
        catch(Exception e){
            System.out.println(e);
        }
        return "";
    }

    public static String eliminatePrefix(String filepath){
        String[] ss = filepath.split("/");
        String[] strings =  filepath.split(ss[0]+'/');
        return  strings[1];
    }

}
